import serial
import time
import binascii


# 循环
def DHT22_485(port_number):
    instructions = "0"
    s = serial.Serial("/dev/ttySC2", 9600)
    a1 = 0
    temperature = 200
    humidity = 200
    if port_number == 1:
        instructions = "01040000000271CB"
    elif port_number == 2:
        instructions = "02040000000271F8"
    while a1 <= 1:
        hex_str = bytes.fromhex(instructions)
        s.write(hex_str)
        n = s.inWaiting()
        a1 = a1 + 1
        if n:
            str1 = binascii.b2a_hex(s.read(n))
            port = str1.decode()
            str_a1 = port[6:10]
            try:
                temperature = int(int(str_a1, base=16) / 10)
            except Exception as e:
                temperature = 200
            str_a2 = port[10:14]
            try:
                humidity = int(int(str_a2, base=16) / 10)
            except Exception as e:
                humidity = 200
        time.sleep(1)
    s.flushInput()
    return temperature, humidity
